<?
// +----------------------------------------------------------------------
// | 套票信息检测
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/02/03
// +----------------------------------------------------------------------


class PackTicket{
	
	
	
	
	
	private $paymode = 0;// 支付方式
	private $advance = 0;// 套票需要提前多少天购买
	private $useDate = 0;// 可预定游玩时间

	
	public function __construct(){
		
	}
	
	// 检查套票
	public function checkEffectivePack($lid){
		// 获取 attribute
		$sql="select l.id,l.attribute,t.ddays,t.pay,t.delaydays,t.order_start,t.order_end from uu_land l left join uu_jq_ticket t on l.id=t.landid where l.id=$lid limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		$attr_str = $GLOBALS['le']->f('attribute');
		// echo $attr_str;
		if($attr_str=='') return true;
		$attr=json_decode($attr_str, true);

		$pack = array(
			'ddays'=>$GLOBALS['le']->f('ddays'),
			'pay'=>$GLOBALS['le']->f('pay'),
			'delaydays'=>$GLOBALS['le']->f('delaydays')
		);
		
		// 获取关联子票
		$child_pids = array();
		foreach($attr as $ct) $child_pids[] = $ct['pid'];
		$count = count($child_pids);
		$child_pids_s = implode(',', $child_pids);
		if($child_pids_s=='') return true;
		$sql="select p.id,t.ddays,t.pay,t.order_start,t.order_end,t.delaytype,t.delaydays from uu_products p left join uu_jq_ticket t on t.pid=p.id where p.apply_limit=1 and p.p_status<6 and p.id in ($child_pids_s) limit $count";
		$GLOBALS['le']->query($sql);
		while($row = $GLOBALS['le']->fetch_assoc()){
			$sales[]=$row;
		}
		if($count!=count($sales)) return false;// 子票有未上架
		
		// 检测套票支付方式
		if(!$this->checkPaysAndDdays($sales)) return false;
		
		// 获取套票的有效时间段
		// print_r($sales);
		$useDate = $this->canUseDate($sales);// 获取时间交集
		print_r($useDate);
		if($useDate['sDate']>$useDate['eDate']){// 如果开始时间大于结束时间，表示无效
			return false;
		}
		// echo $this->advance;
		return true;
	}
	
	// 获取套票有效验证日期区间
	public function canUseDate($sales){
		$playDate = date('Y-m-d 00:00:00', time()+($this->advance * 86400));// 使用时间
		$orderDate = date('Y-m-d 00:00:00');//
		$iniDate = $this->effectiveDateSection($sales[0], $orderDate, $playDate);
		foreach($sales as $key=>$data){
			// if($key==0) continue;
			$data_t = $this->effectiveDateSection($data, $orderDate, $playDate);
			print_r($data_t);
			if($data_t['sDate']>$iniDate['sDate']) $iniDate['sDate'] = $data_t['sDate'];
			if($data_t['eDate']<$iniDate['eDate']) $iniDate['eDate'] = $data_t['eDate'];
			
			// if(!$data_t['section']) continue;
			
			// 如果有效期是时间阶段，有最大游玩日期
			
			
		}
		
		// print_r($iniDate);
		// return $iniDate;
	}
	
	// 获取套票可下单时间
	public function submitOrderDate(){
		
	}
	
	
	// 获取一个子票有效的验证日期区间
	public function effectiveDateSection(Array $data, $orderDate='', $playDate=''){
		// print_r($data);
		$arr = array();
		if($orderDate=='') $orderDate = date('Y-m-d 00:00:000');// 下单时间
		if($playDate=='') $playDate = date('Y-m-d 00:00:000');// 游玩时间
		if($data['order_start']!='' && $data['order_end']){
			$arr['sDate'] = $data['order_start'];
			$arr['eDate'] = $data['order_end'];
			$arr['section'] = 1;
		}else{
			if($data['delaytype']==0){// 游玩时间
				$arr['sDate'] = $playDate;
				$arr['eDate'] = date('Y-m-d 59:59:59',strtotime($arr['sDate']) + $data['delaydays']*86400);
			}else{
				$arr['sDate'] = date('Y-m-d 00:00:00',strtotime($orderDate) + $data['ddays']*86400);
				$arr['eDate'] = date('Y-m-d 59:59:59',strtotime($arr['sDate']) + ($data['delaydays']-$data['ddays'])*86400);
			}
		}
		return $arr;
	}
	
	// 检查套票的支付方式并获取套票提前天数
	public function checkPaysAndDdays($list){
		$this->paymode = $list[0]['pay'];// 支付方式
		$this->advance = $list[0]['ddays'];// 提前购买天数
		foreach($list as $key=>$ct){
			if($ct['pay'] != $this->paymode) return false;// 支付方式不一致
			if($ct['ddays'] > $this->advance)  $this->advance = $ct['ddays'];// 提前天数最大值
		}
		return true;
	}
}



/*
计算套票有效时间，套票有效期及在预定的时间所有子票都可以使用
1.如果第一个子票是时间段
*/



/*
套票有效设定
1.支付方式必须一致
2.套票提前天数为子票的提前天数最大值，但预定的有玩日期所有子票必须有效
*/






// 移除 tarMessage.php 通知信息，一般在套票上架属性没有问题时
function removeMsg($lid){
	$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
	if(file_exists($file)){
		$tmp = include $file;
		unset($tmp['S:'.$lid]);
		$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
			writeMessage($saveArr);
	}
}

// tid 是门票的 pid
function chkPackage($tid,$DB){

	$tarArr = getMainId($tid);// 获取该门票被打包的景区ID
	if(count($tarArr)){// 有关联，获取在售的

		$sql="SELECT l.id,l.attribute,t.ddays,t.pay,t.delaydays FROM uu_land l LEFT JOIN uu_jq_ticket t ON l.id=t.landid WHERE l.status=1 AND l.id IN (".implode(',',$tarArr).")";
        $DB->query($sql);
		
		// 获取套票的landid
		$lands = array();
        $packs = array(); // 套票门票信息
		while($row=$DB->fetch_assoc()){
			$lands[$row['id']] = json_decode($row['attribute']);
            $packs[$row['id']] = array(
                   'ddays'=>$row['ddays'],
                   'pay'=>$row['pay'],
                   'delaydays'=>$row['delaydays']
            );
		};
        // print_r($lands);
		// 获取被打包的子票信息
		$reqState = array();
		foreach($lands as $k=>$v){
			$pidArr = array();
			foreach($v as $val){
				$pidArr[]=$val->pid;
			}
			$sql="SELECT * FROM uu_jq_ticket WHERE pid IN (".implode(',',$pidArr).")";
			$DB->query($sql);
			$proArr = array();
			while($row = $DB->fetch_assoc()){
				$proArr[]=$row;
			}

			if(!chkTicketAttr($proArr,$packs[$k])){
				$reqState['S:'.$k] = array(
					'timer'=>date('Y年m月d日 H:i:s'),
					'message'=>'由于套票的关联子票被修改导致支付方式或有效期、提前天数冲突，系统给予下架，如需上架，请先修改子票或套票属性',
				);
			}
		}
		$tmp = $reqState;
		$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
		if(file_exists($file)){
			$tmp = include $file;
			$tmp = is_array($tmp)? array_merge($tmp,$reqState):$reqState;
		}
		$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
			writeMessage($saveArr);
		return $reqState;
	}
}
// 生成提示信息
function buildMess(Array $reqState){
	$tmp = array();
	$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
	if(file_exists($file)){
		$tmp = include $file;
		$tmp = is_array($tmp)? array_merge($tmp,$reqState):$reqState;
	}
	$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
		writeMessage($saveArr);
}




// 生成提醒套票供应商数据
function writeMessage($txt,$file="/var/www/html/new/d/module/link_product/tarMessage.php"){
	$fp = fopen($file,"w");
	flock($fp, LOCK_EX);
	fwrite($fp,$txt);
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>